config: delete timers by setting 'callback', not 'cb', to nil.
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 if m:plugin_exists("gam-resource-manager") then
54
55 function get_general_priorities(self)
56     print("*** get_general_priorities\n")
57     return { "USB Headset", "wiredHeadset", "speakers" }
58 end
59
60 function get_phone_priorities(self)
61     print("*** get_phone_priorities\n")
62     return { "wiredHeadset", "USB Headset" }
63 end
64
65 m:load_plugin('gam-resource-manager', {
66     config_dir = '/etc/murphy/gam',
67     decision_names = 'gam-wrtApplication-4',
68     max_active = 4,
69     app_mapping = {
70         ['t8j6HTRpuz.MediaPlayer'] = 'wrtApplication',
71         ['pacat'] = 'icoApplication'
72     },
73     app_default = 'icoApplication'
74 })
75
76 routing_sink_priority {
77     application_class = "player",
78     priority_queue = get_general_priorities
79 }
80
81 routing_sink_priority {
82     application_class = "game",
83     priority_queue = get_general_priorities
84 }
85
86 routing_sink_priority {
87     application_class = "implicit",
88     priority_queue = get_general_priorities
89 }
90
91 routing_sink_priority {
92     application_class = "phone",
93     priority_queue = get_phone_priorities
94 }
95
96 routing_sink_priority {
97     application_class = "basic",
98     priority_queue = get_phone_priorities
99 }
100
101 routing_sink_priority {
102     application_class = "event",
103     priority_queue = get_phone_priorities
104 }
105 end
106
107 -- load the AMB plugin
108 if m:plugin_exists('amb') then
109     m:try_load_plugin('amb')
110
111     if builtin.method.amb_initiate and
112        builtin.method.amb_update
113     then
114         with_amb = true
115     end
116 else
117     m:info("No amb plugin found...")
118 end
119
120 -- load the ASM resource plugin
121 if m:plugin_exists('resource-asm') then
122     m:try_load_plugin('resource-asm', {
123         zone = "driver",
124         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
125         ignored_argv0 = "WebProcess"
126     })
127 else
128     m:info("No audio session manager plugin found...")
129 end
130
131 if m:plugin_exists('system-controller') then
132     with_system_controller = true
133 elseif m:plugin_exists('ivi-resource-manager') then
134     m:load_plugin('ivi-resource-manager')
135     with_system_controller = false
136 end
137
138 -- define application classes
139 application_class {
140     name     = "interrupt",
141     priority = 99,
142     modal    = true ,
143     share    = false,
144     order    = "fifo"
145 }
146
147 application_class {
148     name     = "emergency",
149     priority = 80,
150     modal    = false,
151     share    = false,
152     order    = "fifo"
153 }
154 application_class {
155     name     = "system",
156     priority = 52,
157     modal    = false,
158     share    = true,
159     order    = "lifo"
160 }
161 application_class {
162     name     = "alert",
163     priority = 51,
164     modal    = false,
165     share    = false,
166     order    = "fifo"
167 }
168
169 application_class {
170     name     = "navigator",
171     priority = 50,
172     modal    = false,
173     share    = true,
174     order    = "fifo"
175 }
176
177 application_class {
178     name     = "phone",
179     priority = 6 ,
180     modal    = false,
181     share    = true ,
182     order    = "lifo"
183 }
184 application_class {
185     name     = "camera",
186     priority = 5,
187     modal    = false,
188     share    = false,
189     order    = "lifo"
190 }
191
192 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
193 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
194 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
195 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
196 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
197 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
198
199 -- define zone attributes
200 zone.attributes {
201     type = {mdb.string, "common", "rw"},
202     location = {mdb.string, "anywhere", "rw"}
203 }
204
205 -- define zones
206 zone {
207      name = "driver",
208      attributes = {
209          type = "common",
210          location = "front-left"
211      }
212 }
213
214 zone {
215      name = "passanger1",
216      attributes = {
217          type = "private",
218          location = "front-right"
219      }
220 }
221
222 zone {
223      name = "passanger2",
224      attributes = {
225          type = "private",
226          location = "back-left"
227      }
228 }
229
230 zone {
231      name = "passanger3",
232      attributes = {
233          type = "private",
234          location = "back-right"
235      }
236 }
237
238 zone {
239      name = "passanger4",
240      attributes = {
241          type = "private",
242          location = "back-left"
243      }
244 }
245
246
247 -- define resource classes
248 if not m:plugin_exists('ivi-resource-manager') and
249    not with_system_controller and
250    not m:plugin_exists('gam-resource-manager')
251 then
252    resource.class {
253         name = "audio_playback",
254         shareable = true,
255         attributes = {
256             role = { mdb.string, "music", "rw" },
257             pid = { mdb.string, "<unknown>", "rw" },
258             policy = { mdb.string, "relaxed", "rw" },
259             source = { mdb.string, "webkit", "rw" },
260             conn_id = { mdb.unsigned, 0, "rw" }
261         }
262    }
263 end
264
265 if not m:plugin_exists('gam-resource-manager') then
266     resource.class {
267         name = "audio_recording",
268         shareable = true,
269         attributes = {
270              role   = { mdb.string, "music"    , "rw" },
271              pid    = { mdb.string, "<unknown>", "rw" },
272              policy = { mdb.string, "relaxed"  , "rw" }
273          }
274     }
275 end
276
277 resource.class {
278      name = "video_playback",
279      shareable = false,
280 }
281
282 resource.class {
283      name = "video_recording",
284      shareable = false
285 }
286
287 resource.class {
288      name = "speech_recognition",
289      shareable = true
290 }
291
292 resource.class {
293      name = "speech_synthesis",
294      shareable = true
295 }
296
297 -- PulseAudio volume context
298 mdb.table {
299     name = "volume_context",
300     index = { "id" },
301     create = true,
302     columns = {
303         { "id", mdb.unsigned },
304         { "value", mdb.string, 64 },
305     }
306 }
307
308 -- put default volume context to the table
309 mdb.table.volume_context:insert({ id = 1, value = "default" })
310
311 if not m:plugin_exists('ivi-resource-manager') and
312    not with_system_controller
313 then
314     resource.method.veto = {
315         function(zone, rset, grant, owners, req_set)
316             return true
317          end
318     }
319 end
320
321 -- test for creating selections
322 mdb.select {
323            name = "audio_owner",
324            table = "audio_playback_owner",
325            columns = {"application_class"},
326            condition = "zone_name = 'driver'"
327 }
328
329 mdb.select {
330            name = "vehicle_speed",
331            table = "amb_vehicle_speed",
332            columns = {"value"},
333            condition = "key = 'VehicleSpeed'"
334 }
335
336 element.lua {
337    name    = "speed2volume",
338    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
339    outputs = {  mdb.table { name = "speedvol",
340                             index = {"zone", "device"},
341                             columns = {{"zone", mdb.string, 16},
342                                        {"device", mdb.string, 16},
343                                        {"value", mdb.floating}},
344                             create = true
345                            }
346              },
347    oldvolume = 0.0,
348    update  = function(self)
349                 speed = self.inputs.speed.single_value
350                 if (speed) then
351                     volume = (speed - 144.0) / 7.0
352                 else
353                     volume = 0.0
354                 end
355                 diff = volume - self.oldvolume
356                 if (diff*diff > self.inputs.param) then
357                     print("*** element "..self.name.." update "..volume)
358                     self.oldvolume = volume
359                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
360                 end
361              end
362 }
363
364 mdb.select {
365     name = "amb_state",
366     table = "amb_state",
367     columns = { "state" },
368     condition = "id = 0"
369 }
370
371 -- Night mode processing chain
372
373 mdb.select {
374     name = "exterior_brightness",
375     table = "amb_exterior_brightness",
376     columns = { "value" },
377     condition = "key = 'ExteriorBrightness'"
378 }
379
380 element.lua {
381     name    = "nightmode",
382     inputs  = { brightness = mdb.select.exterior_brightness },
383     oldmode = -1;
384     outputs = {
385         mdb.table {
386             name = "amb_nightmode",
387             index = { "id" },
388             create = true,
389             columns = {
390                 { "id", mdb.unsigned },
391                 { "night_mode", mdb.unsigned }
392             }
393         }
394     },
395     update = function(self)
396         -- This is a trivial function to calculate night mode. Later, we will
397         -- need a better threshold value and hysteresis to prevent oscillation.
398
399         brightness = self.inputs.brightness.single_value
400
401         if not brightness then
402             return
403         end
404
405         print("*** element "..self.name.." update brightness: "..brightness)
406
407         if brightness > 300 then
408             mode = 0
409         else
410             mode = 1
411         end
412
413         print("*** resulting mode: ".. mode)
414
415         if not (mode == self.oldmode) then
416             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
417         end
418
419         self.oldmode = mode
420     end
421 }
422
423 mdb.select {
424     name = "select_night_mode",
425     table = "amb_nightmode",
426     columns = { "night_mode" },
427     condition = "id = 0"
428 }
429
430 if with_amb then
431     sink.lua {
432         name = "night_mode",
433         inputs = { NightMode = mdb.select.select_night_mode,
434                    amb_state = mdb.select.amb_state },
435         property = "NightMode",
436         type = "b",
437         initiate = builtin.method.amb_initiate,
438         update = builtin.method.amb_update
439     }
440 end
441
442 -- Night mode general handlers
443
444 if with_system_controller then
445     sink.lua {
446         name = "nightmode_homescreen",
447         inputs = { owner = mdb.select.select_night_mode },
448         initiate = function(self)
449                 -- data = mdb.select.select_night_mode.single_value
450                 return true
451             end,
452         update = function(self)
453                 send_night_mode_to(homescreen)
454             end
455     }
456 end
457
458 -- Driving mode processing chain
459
460 element.lua {
461     name    = "drivingmode",
462     inputs  = { speed = mdb.select.vehicle_speed },
463     oldmode = -1;
464     outputs = {
465         mdb.table {
466             name = "amb_drivingmode",
467             index = { "id" },
468             create = true,
469             columns = {
470                 { "id", mdb.unsigned },
471                 { "driving_mode", mdb.unsigned }
472             }
473         }
474     },
475     update = function(self)
476
477         speed = self.inputs.speed.single_value
478
479         if not speed then
480             return
481         end
482
483         if speed == 0 then
484             mode = 0
485         else
486             mode = 1
487         end
488
489         if not (mode == self.oldmode) then
490             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
491         end
492
493         self.oldmode = mode
494     end
495 }
496
497 mdb.select {
498     name = "select_driving_mode",
499     table = "amb_drivingmode",
500     columns = { "driving_mode" },
501     condition = "id = 0"
502 }
503
504 if with_amb then
505     sink.lua {
506         name = "driving_mode",
507         inputs = { DrivingMode = mdb.select.select_driving_mode,
508                    amb_state = mdb.select.amb_state },
509         property = "DrivingMode",
510         type = "u",
511         initiate = builtin.method.amb_initiate,
512         update = builtin.method.amb_update
513     }
514 end
515
516 -- turn signals (left, right)
517
518 mdb.select {
519     name = "winker",
520     table = "amb_turn_signal",
521     columns = { "value" },
522     condition = "key = 'TurnSignal'"
523 }
524
525 -- define three categories
526
527 mdb.select {
528     name = "undefined_applications",
529     table = "aul_applications",
530     columns = { "appid" },
531     condition = "category = '<undefined>'"
532 }
533
534 mdb.select {
535     name = "basic_applications",
536     table = "aul_applications",
537     columns = { "appid" },
538     condition = "category = 'basic'"
539 }
540
541 mdb.select {
542     name = "entertainment_applications",
543     table = "aul_applications",
544     columns = { "appid" },
545     condition = "category = 'entertainment'"
546 }
547
548 function ft(t)
549     -- filter the object garbage out of the tables
550     ret = {}
551
552     for k,v in pairs(t) do
553         if k ~= "userdata" and k ~= "new" then
554             ret[k] = v
555         end
556     end
557
558     return ret
559 end
560
561 function getApplication(appid)
562     local conf = nil
563
564     -- find the correct local application definition
565
566     for k,v in pairs(ft(application)) do
567         if appid == v.appid then
568             conf = v
569             break
570         end
571     end
572
573     return conf
574 end
575
576 function regulateApplications(t, regulation)
577     for k,v in pairs(ft(t)) do
578
579         -- iterate through the undefined and entertainment apps, see if
580         -- they have been overruled in local config
581
582         local conf = getApplication(v.appid)
583
584         if conf and conf.resource_class ~= "player" then
585             -- override, don't disable
586             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
587         else
588             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
589         end
590     end
591     resource.method.recalc("driver")
592 end
593
594 -- regulation (on), use "select_driving_mode"
595
596 sink.lua {
597     name = "driving_regulation",
598     inputs = { owner = mdb.select.select_driving_mode },
599     initiate = function(self)
600         -- local data = mdb.select.select_driving_mode.single_value
601         return true
602     end,
603     update = function(self)
604         local data = mdb.select.select_driving_mode.single_value
605
606         if verbose > 1 then
607             print("Driving mode updated: " .. tostring(data))
608         end
609
610         if not sc then
611             return true
612         end
613
614         -- tell homescreen that driving mode was updated
615         send_driving_mode_to(homescreen)
616
617         regulateApplications(ft(mdb.select.entertainment_applications), data)
618         regulateApplications(ft(mdb.select.undefined_applications), data)
619
620         return true
621     end
622 }
623 --[[
624 sink.lua {
625     name = "regulated_app_change",
626     inputs = { undef = mdb.select.undefined_applications,
627                entertainment = mdb.select.entertainment_applications },
628     initiate = function(self)
629         return true
630     end,
631     update = function(self)
632         local data = mdb.select.select_driving_mode.single_value
633
634         if not sc then
635             return
636         end
637
638         if verbose > 1 then
639             print("regulated application list was changed")
640         end
641
642         regulateApplications(ft(mdb.select.entertainment_applications), data)
643         regulateApplications(ft(mdb.select.undefined_applications), data)
644
645         return true
646     end
647 }
648 --]]
649
650 -- shift position (parking, reverse, other)
651
652 mdb.select {
653     name = "gear_position",
654     table = "amb_gear_position",
655     columns = { "value" },
656     condition = "key = 'GearPosition'"
657 }
658
659 -- cameras (back, front, left, right)
660
661 element.lua {
662     name    = "camera_state",
663     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
664     oldmode = -1;
665     outputs = {
666         mdb.table {
667             name = "target_camera_state",
668             index = { "id" },
669             create = true,
670             columns = {
671                 { "id", mdb.unsigned },
672                 { "front_camera", mdb.unsigned },
673                 { "back_camera", mdb.unsigned },
674                 { "right_camera", mdb.unsigned },
675                 { "left_camera", mdb.unsigned }
676             }
677         }
678     },
679     update = function(self)
680
681         front_camera = 0
682         back_camera = 0
683         right_camera = 0
684         left_camera = 0
685
686         if self.inputs.gear == 128 then
687             back_camera = 1
688         elseif self.inputs.winker == 1 then
689             right_camera = 1
690         elseif self.inputs.winker == 2 then
691             left_camera = 1
692         end
693
694         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
695
696     end
697 }
698
699 -- system controller test setup
700
701 if not with_system_controller then
702    -- ok, we should have 'audio_playback' defined by now
703    m:try_load_plugin('telephony')
704    return
705 end
706
707 m:load_plugin('system-controller')
708
709 onscreen_counter = 0
710
711 window_manager_operation_names = {
712     [1] = "create",
713     [2] = "destroy"
714 }
715
716 function window_manager_operation_name(oper)
717     local name = window_manager_operation_names[oper]
718     if name then return name end
719     return "<unknown " .. tostring(oper) .. ">"
720 end
721
722 window_operation_names = {
723     [1] = "create",
724     [2] = "destroy",
725     [3] = "name_change",
726     [4] = "visible",
727     [5] = "configure",
728     [6] = "active",
729     [7] = "map",
730     [8] = "hint"
731 }
732
733 function window_operation_name(oper)
734     local name = window_operation_names[oper]
735     if name then return name end
736     return "<unknown " .. tostring(oper) .. ">"
737 end
738
739 layer_operation_names = {
740     [1] = "create",
741     [2] = "destroy",
742     [3] = "visible"
743 }
744
745 function layer_operation_name(oper)
746     local name = layer_operation_names[oper]
747     if name then return name end
748     return "<unknown " .. tostring(oper) .. ">"
749 end
750
751 input_manager_operation_names = {
752     [1] = "create",
753     [2] = "destroy",
754     [3] = "ready"
755 }
756
757 function input_manager_operation_name(oper)
758     local name = input_manager_operation_names[oper]
759     if name then return name end
760     return "<unknown " .. tostring(oper) .. ">"
761 end
762
763 input_operation_names = {
764     [1] = "create",
765     [2] = "destroy",
766     [3] = "update"
767 }
768
769 function input_operation_name(oper)
770     local name = input_operation_names[oper]
771     if name then return name end
772     return "<unknown " .. tostring(oper) .. ">"
773 end
774
775 code_operation_names = {
776     [1] = "create",
777     [2] = "destroy",
778     [3] = "state_change"
779 }
780
781 function code_operation_name(oper)
782     local name = code_operation_names[oper]
783     if name then return name end
784     return "<unknown " .. tostring(oper) .. ">"
785 end
786
787 command_names = {
788     [0x00001] = "send_appid",
789     [0x10001] = "create",
790     [0x10002] = "destroy",
791     [0x10003] = "show",
792     [0x10004] = "hide",
793     [0x10005] = "move",
794     [0x10006] = "animation",
795     [0x10007] = "change_active",
796     [0x10008] = "change_layer",
797     [0x10009] = "change_attr",
798     [0x10010] = "name",
799     [0x10020] = "map_thumb",
800     [0x10021] = "unmap_thumb",
801     [0x10022] = "map_get",
802     [0x10030] = "show layer",
803     [0x10031] = "hide_layer",
804     [0x10032] = "change_layer_attr",
805     [0x20001] = "add_input",
806     [0x20002] = "del_input",
807     [0x30001] = "change_user",
808     [0x30002] = "get_userlist",
809     [0x30003] = "get_lastinfo",
810     [0x30004] = "set_lastinfo",
811     [0x40001] = "acquire_res",
812     [0x40002] = "release_res",
813     [0x40003] = "deprive_res",
814     [0x40004] = "waiting_res",
815     [0x40005] = "revert_res",
816     [0x40006] = "window_id_res",
817     [0x40011] = "create_res",
818     [0x40012] = "destroy_res",
819     [0x50001] = "set_region",
820     [0x50002] = "unset_region",
821     [0x60001] = "change_state"
822 }
823
824 function command_name(command)
825     local name = command_names[command]
826     if name then return name end
827     return "<unknown " .. tostring(command) .. ">"
828 end
829
830 input_layer = {
831    [101] = true, -- input
832    [102] = true, -- touch
833    [103] = true  -- cursor
834 }
835
836 -- some day this should be merged with wmgr.layers
837 ico_layer_type = {
838    [1]   = 0x1000, -- background
839    [2]   = 0x2000, -- application
840    [3]   = 0x2000, -- homescreen
841    [4]   = 0x2000, -- interrupt application
842    [5]   = 0x2000, -- onscreen application
843    [6]   = 0xc000, -- startup
844    [7]   = 0x3000, -- fullscreen
845    [101] = 0x4000, -- input
846    [102] = 0xa000, -- touch
847    [103] = 0xb000  -- cursor
848 }
849
850 resmgr = resource_manager {
851   screen_event_handler = function(self, ev)
852                              local event = ev.event
853                              local surface = ev.surface
854
855                              if event == "init" then
856                                  if verbose > 0 then
857                                      print("*** init screen resource allocation -- disable all 'player'")
858                                  end
859                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
860                              elseif event == "preallocate" then
861                                  if verbose > 0 then
862                                      print("*** preallocate screen resource "..
863                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
864                                  end
865                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
866                              elseif event == "grant" then
867                                  if verbose > 0 then
868                                     print("*** make visible surface "..surface)
869                                  end
870                                  local a = animation({})
871                                  local r = m:JSON({surface = surface,
872                                                    visible = 1,
873                                                    raise   = 1})
874                                  if ev.appid == onscreen then
875                                      onscreen_counter = onscreen_counter + 1
876                                      wmgr:layer_request(m:JSON({layer = 5, visible = 1}))
877                                  end
878
879                                  wmgr:window_request(r,a,0)
880                              elseif event == "revoke" then
881                                  if verbose > 0 then
882                                     print("*** hide surface "..surface)
883                                  end
884                                  local a = animation({})
885                                  local r = m:JSON({surface = ev.surface,
886                                                    visible = 0})
887                                  if ev.appid == onscreen then
888                                      onscreen_counter = onscreen_counter - 1
889                                      if onscreen_counter <= 0 then
890                                         onscreen_counter = 0
891                                         wmgr:layer_request(m:JSON({layer = 5, visible = 0}))
892                                      end
893                                  end
894                                  wmgr:window_request(r,a,0)
895
896                              elseif event == "create" then
897
898                                 if verbose > 0 then
899                                     print("*** screen resource event: " ..
900                                           tostring(ev))
901                                 end
902
903                                 local regulation = mdb.select.select_driving_mode.single_value
904
905                                 if regulation == 1 then
906
907                                     local blacklisted = false
908
909                                     -- applications which have their category set to "entertainment"
910                                     -- or "undefined" are blacklisted, meaning they should be regulated
911
912                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
913                                         if v.appid == ev.appid then
914                                             if verbose > 0 then
915                                                 print(ev.appid .. " was blacklisted (undefined)")
916                                             end
917                                             blacklisted = true
918                                             break
919                                         end
920                                     end
921
922                                     if not blacklisted then
923                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
924                                             if v.appid == ev.appid then
925                                                 if verbose > 0 then
926                                                     print(ev.appid .. " was blacklisted (entertainment)")
927                                                 end
928                                                 blacklisted = true
929                                                 break
930                                             end
931                                         end
932                                     end
933
934                                     -- our local application config, which takes precedence
935                                     local conf = getApplication(ev.appid)
936
937                                     -- disable only non-whitelisted applications
938                                     if not conf or conf.resource_class == "player" then
939                                         if blacklisted then
940                                             if verbose > 0 then
941                                                 print("disabling screen for " .. ev.appid)
942                                             end
943                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
944                                         end
945                                     end
946                                 end
947
948                              elseif event == "destroy" then
949                                if verbose > 0 then
950                                     print("*** screen resource event: " ..
951                                           tostring(ev))
952                                  end
953                              else
954                                  if verbose > 0 then
955                                     print("*** screen resource event: " ..
956                                           tostring(ev))
957                                  end
958                              end
959                          end,
960   audio_event_handler = function(self, ev)
961                              local event = ev.event
962                              local appid = ev.appid
963                              local audioid = ev.audioid
964
965                              if event == "grant" then
966                                  if verbose > 0 then
967                                     print("*** grant audio to "..appid..
968                                           " ("..audioid..") in '" ..
969                                           ev.zone .. "' zone")
970                                  end
971                              elseif event == "revoke" then
972                                  if verbose > 0 then
973                                     print("*** revoke audio from "..appid..
974                                           " ("..audioid..") in '" ..
975                                           ev.zone .. "' zone")
976                                  end
977                              else
978                                  if verbose > 0 then
979                                     print("*** audio resource event: " ..
980                                           tostring(ev))
981                                  end
982                              end
983                         end
984 }
985
986 resclnt = resource_client {}
987
988 wmgr = window_manager {
989   geometry = function(self, w,h, v)
990                   if type(v) == "function" then
991                       return v(w,h)
992                   end
993                   return v
994              end,
995
996   application = function(self, appid)
997                      if appid then
998                          local app = application_lookup(appid)
999                          if not app then
1000                              app = application_lookup("default")
1001                          end
1002                          return app
1003                      end
1004                      return { privileges = {screen="none", audio="none"} }
1005                 end,
1006
1007   output_order = { 1, 0 },
1008
1009   outputs = { { name  = "Mid",
1010                 id    = 1,
1011                 zone  = "driver",
1012                 areas = { Full = {
1013                               id     = 20,
1014                               pos_x  = 0,
1015                               pos_y  = 0,
1016                               width  = function(w,h) return w end,
1017                               height = function(w,h) return h end
1018                           },
1019                           Left = {
1020                               id     = 21,
1021                               pos_x  = 0,
1022                               pos_y  = 0,
1023                               width  = 320,
1024                               height = function(w,h) return h end
1025                           },
1026                           Right = {
1027                               id     = 22,
1028                               pos_x  = function(w,h) return w-320 end,
1029                               pos_y  = 0,
1030                               width  = 320,
1031                               height = function(w,h) return h end
1032                           }
1033                         }
1034                },
1035                { name  = "Center",
1036                  id    = 4,
1037                  zone  = "driver",
1038                  areas = { Status = {
1039                               id     = 0,
1040                               pos_x  = 0,
1041                               pos_y  = 0,
1042                               width  = function(w,h) return w end,
1043                               height = 64
1044                            },
1045                            Full = {
1046                               id     = 1,
1047                               pos_x  = 0,
1048                               pos_y  = 64,
1049                               width  = function(w,h) return w end,
1050                               height = function(w,h) return h-64-128 end
1051                            },
1052                            Upper = {
1053                               id     = 2,
1054                               pos_x  = 0,
1055                               pos_y  = 64,
1056                               width  = function(w,h) return w end,
1057                               height = function(w,h) return (h-64-128)/2 end
1058                            },
1059                            Lower = {
1060                               id     = 3,
1061                               pos_x  = 0,
1062                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
1063                               width  = function(w,h) return w end,
1064                               height = function(w,h) return (h-64-128)/2 end
1065                            },
1066                            UpperLeft = {
1067                               id     = 4,
1068                               pos_x  = 0,
1069                               pos_y  = 64,
1070                               width  = function(w,h) return w/2 end,
1071                               height = function(w,h) return (h-64-128)/2 end
1072                            },
1073                            UpperRight = {
1074                               id     = 5,
1075                               pos_x  = function(w,h) return w/2 end,
1076                               pos_y  = 64,
1077                               width  = function(w,h) return w/2 end,
1078                               height = function(w,h) return (h-64-128)/2 end
1079                            },
1080                            LowerLeft = {
1081                               id     = 6,
1082                               pos_x  = 0,
1083                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1084                               width  = function(w,h) return w/2 end,
1085                               height = function(w,h) return (h-64-128)/2 end
1086                            },
1087                            LowerRight = {
1088                               id     = 7,
1089                               pos_x  = function(w,h) return w/2 end,
1090                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1091                               width  = function(w,h) return w/2 end,
1092                               height = function(w,h) return (h-64-128)/2 end
1093                            },
1094                            SysApp = {
1095                               id     = 8,
1096                               pos_x  = 0,
1097                               pos_y  = 64,
1098                               width  = function(w,h) return w end,
1099                               height = function(w,h) return h-64-128 end
1100                            },
1101                            ["SysApp.Left"] = {
1102                               id     = 9,
1103                               pos_x  = 0,
1104                               pos_y  = 64,
1105                               width  = function(w,h) return w/2-181 end,
1106                               height = function(w,h) return h-64-128 end
1107                            },
1108                            ["SysApp.Right"] = {
1109                               id     = 10,
1110                               pos_x  = function(w,h) return w/2+181 end,
1111                               pos_y  = 64,
1112                               width  = function(w,h) return w/2-181 end,
1113                               height = function(w,h) return h-64-128 end
1114                            },
1115                            MobileFull = {
1116                               id     = 11,
1117                               pos_x  = 0,
1118                               pos_y  = 64,
1119                               width  = function(w,h) return w end,
1120                               height = function(w,h) return h-64-128 end
1121                            },
1122                            MobileUpper = {
1123                               id     = 12,
1124                               pos_x  = 0,
1125                               pos_y  = 64,
1126                               width  = function(w,h) return w end,
1127                               height = function(w,h) return (h-64-128)/2 end
1128                            },
1129                            MobileLower = {
1130                               id     = 13,
1131                               pos_x  = 0,
1132                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
1133                               width  = function(w,h) return w end,
1134                               height = function(w,h) return (h-64-128)/2 end
1135                            },
1136                            Control = {
1137                               id     = 14,
1138                               pos_x  = 0,
1139                               pos_y  = function(w,h) return h-128 end,
1140                               width  = function(w,h) return w end,
1141                               height = 128
1142                            },
1143                         }
1144               }
1145   },
1146              --    id   name            type  output
1147   layers = { {      0, "BackGround"   ,    1, "Center" },
1148              {      1, "Application"  ,    2, "Center" },
1149              {      2, "HomeScreen"   ,    3, "Center" },
1150              {      3, "ControlBar"   ,    3, "Center" },
1151              {      4, "InterruptApp" ,    4, "Center" },
1152              {      5, "OnScreen"     ,    5, "Center" },
1153              {      6, "Touch"        ,  102, "Center" },
1154              {      7, "Cursor"       ,  103, "Center" }
1155   },
1156
1157
1158   manager_update = function(self, oper)
1159                        if verbose > 0 then
1160                            print("### <== WINDOW MANAGER UPDATE:" ..
1161                                  window_manager_operation_name(oper))
1162                        end
1163                        if oper == 1 then
1164                            local wumask = window_mask { --raise   = true,
1165                                                         visible = true,
1166                                                         active  = true }
1167                            local wrmask = window_mask { raise   = true,
1168                                                         active  = true,
1169                                                         layer   = true }
1170                            local lumask = layer_mask  { visible = true }
1171                            local lrmask = layer_mask  { visible = true }
1172                            local req = m:JSON({
1173                                passthrough_window_update  = wumask:tointeger(),
1174                                passthrough_window_request = wrmask:tointeger(),
1175                                passthrough_layer_update   = lumask:tointeger(),
1176                                passthrough_layer_request  = lrmask:tointeger()
1177                            })
1178                            self:manager_request(req)
1179                        end
1180                    end,
1181
1182   window_update = function(self, oper, win, mask)
1183                       if verbose > 0 then
1184                           print("### <== WINDOW UPDATE oper:" ..
1185                                 window_operation_name(oper) ..
1186                                 " mask: " .. tostring(mask))
1187                           if verbose > 1 then
1188                               print(win)
1189                           end
1190                       end
1191
1192                       local arg = m:JSON({ surface = win.surface,
1193                                            winname = win.name
1194                       })
1195                       local command = 0
1196
1197                       if oper == 1 then -- create
1198                            local layertype = win.layertype
1199                            if layertype and input_layer[layertype] then
1200                                if verbose > 0 then
1201                                    print("ignoring input panel creation")
1202                                end
1203                                return
1204                            end
1205                            command     = 0x10001
1206                       elseif oper == 2 then -- destroy
1207                            command     = 0x10002
1208                       elseif oper == 3 then  -- namechange
1209                            command     = 0x10010
1210                       elseif oper == 4 or oper == 5 then -- visible/configure
1211                            command     = 0x10009
1212                            arg.zone    = win.area
1213                            arg.node    = win.node
1214                            if win.layertype then
1215                                arg.layertype = win.layertype
1216                            end
1217                            arg.layer   = win.layer
1218                            arg.pos_x   = win.pos_x
1219                            arg.pos_y   = win.pos_y
1220                            arg.width   = win.width
1221                            arg.height  = win.height
1222                            arg.raise   = win.raise
1223                            arg.visible = win.visible
1224                            if win.active == 0 then
1225                                arg.active = 0
1226                            else
1227                                arg.active = 1
1228                            end
1229                       elseif oper == 6 then -- active
1230                            if win.active == 0 then
1231                                if verbose > 0 then
1232                                    print("ignoring inactive event")
1233                                end
1234                                return
1235                            end
1236                            command = 0x10007
1237                       elseif oper == 7 then -- map
1238                            local map = win.map
1239                            if not map then
1240                                return
1241                            end
1242                            if win.mapped == 0 then
1243                                command = 0x10021
1244                            else
1245                                command = 0x10020
1246                            end
1247                            arg.attr = map.type
1248                            --arg.name = map.target
1249                            arg.width = map.width
1250                            arg.height = map.height
1251                            arg.stride = map.stride
1252                            arg.format = map.format
1253                       else
1254                            if verbose > 0 then
1255                                print("### nothing to do")
1256                            end
1257                            return
1258                       end
1259
1260                       local msg = m:JSON({ command = command,
1261                                            appid   = win.appid,
1262                                            pid     = win.pid,
1263                                            arg     = arg
1264                       })
1265                       if verbose > 0 then
1266                           print("### <== sending " ..
1267                                 command_name(msg.command) ..
1268                                 " window message to '" .. homescreen .. "'")
1269                           if verbose > 1 then
1270                               print(msg)
1271                           end
1272                       end
1273                       sc:send_message(homescreen, msg)
1274
1275                       if oper == 1 then -- create
1276                           local i = input_layer[win.layertype]
1277                           local p = self:application(win.appid)
1278                           local s = p.privileges.screen
1279
1280                           if s == "system" then
1281                               local a = animation({})
1282                               local r = m:JSON({surface = win.surface,
1283                                                 visible = 0,
1284                                                 raise   = 1})
1285                               self:window_request(r,a,0)
1286                           else
1287                               if i then
1288                                   if verbose > 0 then
1289                                       print("do not make resource for " ..
1290                                             "input window")
1291                                   end
1292                               else
1293                                   resclnt:resource_set_create("screen",
1294                                                                "driver",
1295                                                                win.appid,
1296                                                                win.surface)
1297                                   special_screen_sets[win.surface] = true
1298                               end
1299                           end
1300
1301                           if onscreen and win.appid == onscreen then
1302                               local resmsg = m:JSON({
1303                                         command = 0x40006, -- window_id_res
1304                                         appid   = win.appid,
1305                                         pid     = win.pid,
1306                                         res     = m:JSON({
1307                                             window  = m:JSON({
1308                                                 ECU     = "",
1309                                                 display = "",
1310                                                 layer   = "",
1311                                                 layout  = "",
1312                                                 area    = "",
1313                                                 dispatchApp = "",
1314                                                 role        = win.name,
1315                                                 resourceId  = win.surface
1316                                             })
1317                                         })
1318                               })
1319                               if verbose > 0 then
1320                                   print("### <== sending " ..
1321                                         command_name(resmsg.command) ..
1322                                         " message to '" .. onscreen .. "'")
1323                                   if verbose > 1 then
1324                                       print(resmsg)
1325                                   end
1326                               end
1327                               sc:send_message(onscreen, resmsg);
1328                           end
1329                       elseif oper == 2 then -- destroy
1330                           resclnt:resource_set_destroy("screen", win.surface)
1331                           special_screen_sets[win.surface] = nil
1332                       elseif oper == 6 then -- active
1333                           if win.active then
1334                               local i = input_layer[win.layertype]
1335                               local p = self:application(win.appid)
1336                               local s = p.privileges.screen
1337                               local surface = win.surface
1338                               if not i and s ~= "system" then
1339                                  resclnt:resource_set_acquire("screen",surface)
1340                                  resmgr:window_raise(win.appid, surface, 1)
1341                               end
1342                           end
1343                       end
1344                   end,
1345
1346   layer_update = function(self, oper, layer, mask)
1347                       if verbose > 0 then
1348                           print("### LAYER UPDATE:" ..
1349                                 layer_operation_name(oper) ..
1350                                 " mask: " .. tostring(mask))
1351                           if verbose > 1 then
1352                               print(layer)
1353                           end
1354                       end
1355                       if oper == 3 then -- visible
1356                          local command = 0x10008 -- change_layer
1357                          local msg = m:JSON({
1358                                          command = command,
1359                                          appid = "",
1360                                          arg = m:JSON({layer = layer.id,
1361                                                        visible = layer.visible
1362                                          })
1363                                 })
1364                          if verbose > 0 then
1365                             print("### <== sending "..command_name(command)..
1366                                   " layer message")
1367                             if verbose > 1 then
1368                                 print(msg)
1369                             end
1370                          end
1371                          sc:send_message(homescreen, msg)
1372                       else
1373                            if verbose > 0 then
1374                                print("### nothing to do")
1375                            end
1376                       end
1377                  end,
1378
1379   output_update = function(self, oper, out, mask)
1380                       local idx = out.index
1381                       local defidx = self.output_order[idx+1]
1382                       if verbose > 0 then
1383                           print("### OUTPUT UPDATE:" .. oper ..
1384                                 " mask: "..tostring(mask))
1385                       end
1386                       if not defidx then
1387                           return
1388                       end
1389                       print(out)
1390                       local outdef = self.outputs[defidx+1]
1391                       if (oper == 1) then -- create
1392                           if outdef then
1393                               self:output_request(m:JSON({index = idx,
1394                                                           id    = outdef.id,
1395                                                           name  = outdef.name
1396                                                           }))
1397                           end
1398                       elseif (oper == 5) then -- done
1399                           local ads = outdef.areas
1400                           local on = outdef.name
1401                           if ads then
1402                               for name,ad in pairs(ads) do
1403                                   local can = wmgr:canonical_name(on.."."..name)
1404                                   local a = m:JSON({name   = name,
1405                                                     output = out.index})
1406                                   for fld,val in pairs(ad) do
1407                                       a[fld] = self:geometry(out.width,
1408                                                              out.height,
1409                                                              val)
1410                                    end
1411                                    self:area_create(a)
1412                                    resmgr:area_create(area[can], outdef.zone)
1413                               end
1414                           end
1415                       end
1416                   end
1417 }
1418
1419
1420 imgr = input_manager {
1421   inputs = {{ name = "G27 Racing Wheel",
1422               id = 0,
1423               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1424                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1425                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1426                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1427              }}
1428   },
1429
1430   manager_update = function(self, oper)
1431                        if verbose > 0 then
1432                            print("### <== INPUT MANAGER UPDATE:" ..
1433                                  input_manager_operation_name(oper))
1434                        end
1435                    end,
1436
1437   input_update = function(self, oper, inp, mask)
1438                      if verbose > 0 then
1439                          print("### INPUT UPDATE:" ..
1440                                 input_operation_name(oper) ..
1441                                 " mask: " .. tostring(mask))
1442                           if verbose > 1 then
1443                               print(inp)
1444                           end
1445                       end
1446                  end,
1447   code_update  = function(self, oper, code, mask)
1448                      if verbose > 0 then
1449                          print("### CODE UPDATE: mask: " .. tostring(mask))
1450                          if verbose > 1 then
1451                              print(code)
1452                          end
1453                      end
1454                      local msg = m:JSON({ command = 1,
1455                                           appid = "org.tizen.ico.homescreen",
1456                                           arg = m:JSON({ device = code.device,
1457                                                          time = code.time,
1458                                                          input = code.input,
1459                                                          code = code.id,
1460                                                          state = code.state
1461                                            })
1462                      })
1463                      if verbose > 0 then
1464                          print("### <== sending " ..
1465                                command_name(msg.command) ..
1466                                " input message")
1467                          if verbose > 1 then
1468                              print(msg)
1469                          end
1470                      end
1471                      sc:send_message(homescreen, msg)
1472                  end
1473 }
1474
1475 sc = m:get_system_controller()
1476
1477 -- resource sets
1478 sets = {}
1479
1480 -- special screen resource sets
1481 -- TODO: just rewrite screen resource handling to use regular resource API
1482
1483 special_screen_sets = {}
1484
1485 -- user manager
1486 um = m:UserManager()
1487
1488 connected = false
1489 homescreen = ""
1490 onscreen = ""
1491
1492 cids = {}
1493
1494 -- these shoud be before wmgr:connect() is called
1495 if verbose > 0 then
1496    print("====== creating applications ======")
1497 end
1498 application {
1499     appid          = "default",
1500     area           = "Center.Full",
1501     privileges     = { screen = "none", audio = "none" },
1502     resource_class = "player",
1503     screen_priority = 0
1504 }
1505
1506 application {
1507     appid           = "weston",
1508     area            = "Center.Full",
1509     privileges      = { screen = "system", audio = "none" },
1510     resource_class  = "implicit",
1511     screen_priority = 30
1512 }
1513
1514 application {
1515     appid           = "org.tizen.ico.homescreen",
1516     area            = "Center.Full",
1517     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1518     privileges      = { screen = "system", audio = "system" },
1519     resource_class  = "player",
1520     screen_priority = 20
1521 }
1522
1523 application {
1524     appid           = "org.tizen.ico.statusbar",
1525     area            = "Center.Status",
1526     privileges      = { screen = "system", audio = "none" },
1527     resource_class  = "player",
1528     screen_priority = 20
1529 }
1530
1531 application {
1532     appid           = "org.tizen.ico.onscreen",
1533     area            = "Center.Full",
1534     privileges      = { screen = "system", audio = "system" },
1535     resource_class  = "player",
1536     screen_priority = 20
1537 }
1538
1539 application {
1540     appid           = "org.tizen.ico.login",
1541     area            = "Center.Full",
1542     privileges      = { screen = "system", audio = "system" },
1543     resource_class  = "player",
1544     screen_priority = 20
1545 }
1546
1547 application {
1548     appid           = "org.tizen.ico.camera_left",
1549     area            = "Center.SysApp.Left",
1550     privileges      = { screen = "system", audio = "none" },
1551     requisites      = { screen = "blinker_left", audio = "none" },
1552     resource_class  = "player",
1553     screen_priority = 30
1554 }
1555
1556 application {
1557     appid           = "org.tizen.ico.camera_right",
1558     area            = "Center.SysApp.Right",
1559     privileges      = { screen = "system", audio = "none" },
1560     requisites      = { screen = "blinker_right", audio = "none" },
1561     resource_class  = "player",
1562     screen_priority = 30
1563 }
1564
1565 application {
1566     appid           = "net.zmap.navi",
1567     area            = "Center.Full",
1568     privileges      = { screen = "none", audio = "none" },
1569     resource_class  = "navigator",
1570     screen_priority = 30
1571 }
1572
1573 application {
1574     appid           = "GV3ySIINq7.GhostCluster",
1575     area            = "Center.Full",
1576     privileges      = { screen = "none", audio = "none" },
1577     resource_class  = "system",
1578     screen_priority = 30
1579 }
1580
1581 if sc then
1582     sc.client_handler = function (self, cid, msg)
1583         local command = msg.command
1584         local appid = msg.appid
1585         if verbose > 0 then
1586             print('### ==> client handler:')
1587             if verbose > 1 then
1588                 print(msg)
1589             end
1590         end
1591
1592         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1593         -- disconnection
1594
1595         if command == 0xFFFF then
1596             if verbose > 1 then
1597                 print('client ' .. cid .. ' disconnected')
1598             end
1599             if msg.appid == homescreen then
1600                 homescreen = ""
1601                 for i,v in pairs(special_screen_sets) do
1602                     resclnt:resource_set_destroy("screen", i)
1603                     special_screen_sets[i] = nil
1604                 end
1605             end
1606             return
1607         end
1608
1609         -- handle the connection to weston
1610
1611         if appid then
1612             if appid == "org.tizen.ico.homescreen" then
1613                 print('Setting homescreen='..appid)
1614                 homescreen = appid
1615                 if command and command == 1 then
1616                     send_driving_mode_to(homescreen)
1617                     send_night_mode_to(homescreen)
1618                 end
1619             elseif appid == "org.tizen.ico.onscreen" then
1620                 onscreen = appid
1621                 if command and command == 1 then
1622                     send_driving_mode_to(onscreen)
1623                     send_night_mode_to(onscreen)
1624                 end
1625             end
1626
1627         if not connected and appid == "org.tizen.ico.homescreen" then
1628                 print('Trying to connect to weston...')
1629                 connected = wmgr:connect()
1630             end
1631             cids[cid] = appid
1632         end
1633     end
1634
1635     sc.generic_handler = function (self, cid, msg)
1636         if verbose > 0 then
1637             print('### ==> generic handler:')
1638             if verbose > 1 then
1639                print(msg)
1640             end
1641         end
1642     end
1643
1644     sc.window_handler = function (self, cid, msg)
1645         if verbose > 0 then
1646             print('### ==> received ' ..
1647                    command_name(msg.command) .. ' message from ' .. cids[cid])
1648             if verbose > 1 then
1649                 print(tostring(msg))
1650             end
1651         end
1652
1653         local a = animation({})
1654         local nores = false
1655         if msg.command == 0x10003 then       -- ico SHOW command
1656             local raise_mask = 0x01000000
1657             local lower_mask = 0x02000000
1658             local nores_mask = 0x40000000
1659             local time_mask  = 0x00ffffff
1660             msg.arg.visible = 1
1661             if msg.arg then
1662                 local time = 200
1663                 if  msg.arg.anim_time then
1664                     local t = msg.arg.anim_time
1665                     time = m:AND(t, time_mask)
1666                     nores = not m:AND(t, nores_mask)
1667                     if m:AND(t, raise_mask) then
1668                         msg.arg.raise = 1
1669                     elseif m:AND(t, lower_mask) then
1670                         msg.arg.raise = 0
1671                     end
1672                 end
1673                 if msg.arg.anim_name then
1674                     a.show = { msg.arg.anim_name, time }
1675                     print('time: ' .. tostring(time))
1676                 end
1677             end
1678             if not nores then
1679                 local p = wmgr:application(msg.appid)
1680                 local s = p.privileges.screen
1681                 if s == "system" then
1682                     nores = true
1683                     if not msg.arg.raise then
1684                         msg.arg.raise = 1
1685                     end
1686                 end
1687             end
1688             if verbose > 2 then
1689                 print('### ==> SHOW')
1690                 print(tostring(msg.arg))
1691             end
1692             if nores then
1693                 wmgr:window_request(msg.arg, a, 0)
1694             else
1695                 local surface = msg.arg.surface
1696                 resclnt:resource_set_acquire("screen", surface)
1697                 resmgr:window_raise(msg.appid, surface, 1)
1698             end
1699         elseif msg.command == 0x10004 then   -- ico HIDE command
1700             local raise_mask = 0x01000000
1701             local lower_mask = 0x02000000
1702             local nores_mask = 0x40000000
1703             local time_mask  = 0x00ffffff
1704             msg.arg.visible = 0
1705             if msg.arg then
1706                 local time = 200
1707                 if msg.arg.anim_time then
1708                     local t = msg.arg.anim_time
1709                     time = m:AND(t, time_mask)
1710                     nores = not m:AND(t, nores_mask)
1711                 end
1712                 if msg.arg.anim_name then
1713                     a.hide = { msg.arg.anim_name, time }
1714                     print('hide animation time: ' .. tostring(a.hide[2]))
1715                 end
1716             end
1717             if not nores then
1718                 local p = wmgr:application(msg.appid)
1719                 local s = p.privileges.screen
1720                 if s == "system" then
1721                     nores = true
1722                     msg.arg.raise = 0
1723                 end
1724             end
1725             if verbose > 2 then
1726                 print('### ==> HIDE REQUEST')
1727                 print(tostring(msg.arg))
1728             end
1729             if nores then
1730                 wmgr:window_request(msg.arg, a, 0)
1731             else
1732                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1733             end
1734         elseif msg.command == 0x10005 then   -- ico MOVE
1735             if verbose > 2 then
1736                 print('### ==> MOVE REQUEST')
1737                 print(tostring(msg.arg))
1738             end
1739             if msg.arg.zone then
1740                 msg.arg.area = msg.arg.zone
1741             end
1742             wmgr:window_request(msg.arg, a, 0)
1743             -- TODO: handle if area changed
1744         elseif msg.command == 0x10007 then   -- ico CHANGE_ACTIVE
1745             if not msg.arg.active then
1746                 msg.arg.active = 3 -- pointer + keyboard
1747             end
1748             if verbose > 2 then
1749                 print('### ==> CHANGE_ACTIVE REQUEST')
1750                 print(tostring(msg.arg))
1751             end
1752             wmgr:window_request(msg.arg, a, 0)
1753         elseif msg.command == 0x10008 then   -- ico CHANGE_LAYER
1754             if verbose > 2 then
1755                 print('### ==> CHANGE_LAYER REQUEST')
1756                 print(tostring(msg.arg))
1757             end
1758             --[[
1759             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1760                 print("do not change layer for other than cursor or touch")
1761                 return
1762             end
1763             --]]
1764             wmgr:window_request(msg.arg, a, 0)
1765         elseif msg.command == 0x10020 then   -- ico MAP_THUMB
1766             local framerate = msg.arg.framerate
1767             local animname  = msg.arg.anim_name
1768             if animname then
1769                a.map = { animname, 1 }
1770                if not framerate or framerate < 0 then
1771                   framerate = 5
1772                end
1773                msg.arg.mapped = 1
1774                if verbose > 2 then
1775                   print('### ==> MAP_THUMB REQUEST')
1776                   print(msg.arg)
1777                   print('framerate: '..framerate)
1778                end
1779                wmgr:window_request(msg.arg, a, framerate)
1780             end
1781         elseif msg.command == 0x10021 then   -- ico UNMAP_THUMB
1782             msg.arg.mapped = 0
1783             if verbose > 2 then
1784                 print('### ==> UNMAP_THUMB REQUEST')
1785                 print(msg.arg)
1786             end
1787             wmgr:window_request(msg.arg, a, 0)
1788 --[[
1789         elseif msg.command == 0x10013 then -- ico MAP_BUFFER command
1790             local shmname = msg.arg.anim_name
1791             local bufsize = msg.arg.width
1792             local bufnum  = msg.arg.height
1793             if shmname and bufsize and bufnum then
1794                 if verbose > 2 then
1795                     print('### ==> MAP_BUFFER REQUEST')
1796                     print("shmaname='" .. shmname ..
1797                           "' bufsize='" .. bufsize ..
1798                           " bufnum=" .. bufnum)
1799                 end
1800                 wmgr:buffer_request(shmname, bufsize, bufnum)
1801             end
1802 --]]
1803         elseif msg.command == 0x10030 then   -- ico SHOW_LAYER command
1804             msg.arg.visible = 1
1805             if verbose > 2 then
1806                 print('### ==> SHOW_LAYER REQUEST')
1807                 print(msg.arg)
1808             end
1809             wmgr:layer_request(msg.arg)
1810         elseif msg.command == 0x10031 then   -- ico HIDE_LAYER command
1811             msg.arg.visible = 0
1812             if verbose > 2 then
1813                 print('### ==> HIDE_LAYER REQUEST')
1814                 print(msg.arg)
1815             end
1816             wmgr:layer_request(msg.arg)
1817         end
1818     end
1819
1820     sc.input_handler = function (self, cid, msg)
1821         if verbose > 0 then
1822             print('### ==> input handler: ' .. command_name(msg.command))
1823             if verbose > 1 then
1824                 print(msg)
1825             end
1826         end
1827         if msg.command == 0x20001 then -- add_input
1828             msg.arg.appid = msg.appid
1829             if verbose > 2 then
1830                 print('### ==> ADD_INPUT REQUEST')
1831                 print(tostring(msg.arg))
1832             end
1833             imgr:input_request(msg.arg)
1834         elseif msg.command == 0x20002 then -- del_input
1835             msg.arg.appid = ''
1836             if verbose > 2 then
1837                 print('### ==> DEL_INPUT REQUEST')
1838                 print(tostring(msg.arg))
1839             end
1840             imgr:input_request(msg.arg)
1841         -- elseif msg.command == 0x20003 then -- send_input
1842         end
1843     end
1844
1845     sc.user_handler = function (self, cid, msg)
1846         if verbose > 0 then
1847             print('### ==> user handler: ' .. command_name(msg.command))
1848             if verbose > 1 then
1849                 print(msg)
1850             end
1851         end
1852
1853         if not um then
1854             print("User Manager not initialized")
1855             return
1856         end
1857
1858         if msg.command == 0x30001 then -- change_user
1859             print("command CHANGE_USER")
1860             if not msg.arg then
1861                 print("invalid message")
1862                 return
1863             end
1864
1865             username = msg.arg.user
1866             passwd = msg.arg.pass
1867
1868             if not username then
1869                 username = ""
1870             end
1871
1872             if not passwd then
1873                 passwd = ""
1874             end
1875
1876             success = um:changeUser(username, passwd)
1877
1878             if not success then
1879                 reply = m.JSON({
1880                     appid = msg.appid,
1881                     command = cmd
1882                 })
1883                 if sc:send_message(msg.appid, reply) then
1884                     print('*** sent authentication failed message')
1885                 else
1886                     print('*** failed to send authentication failed message')
1887                 end
1888             end
1889
1890         elseif msg.command == 0x30002 then -- get_userlist
1891             print("command GET_USERLIST")
1892             if not msg.appid then
1893                 print("invalid message")
1894                 return
1895             end
1896
1897             users, currentUser = um:getUserList()
1898
1899             if not users then
1900                 print("failed to get user list")
1901                 return
1902             end
1903
1904             nUsers = 0
1905
1906             for i,v in pairs(users) do
1907                 nUsers = nUsers + 1
1908             end
1909
1910             if not currentUser then
1911                 currentUser = ""
1912             end
1913
1914             if verbose > 1 then
1915                 print("current user: " .. currentUser)
1916                 print("user list:")
1917                 for i,v in pairs(users) do
1918                     print(v)
1919                 end
1920             end
1921
1922             reply = m.JSON({
1923                 appid = msg.appid,
1924                 command = cmd,
1925                 arg = m.JSON({
1926                     user_num = nUsers,
1927                     user_list = users,
1928                     user_login = currentUser
1929                 })
1930             })
1931
1932             if verbose > 1 then
1933                 print("### <== GetUserList reply: " .. tostring(reply))
1934             end
1935
1936             if sc:send_message(msg.appid, reply) then
1937                 print('*** reply OK')
1938             else
1939                 print('*** reply FAILED')
1940             end
1941
1942         elseif msg.command == 0x30003 then -- get_lastinfo
1943             print("command GET_LASTINFO")
1944             if not msg.appid then
1945                 print("invalid message")
1946                 return
1947             end
1948
1949             lastInfo = um:getLastInfo(msg.appid)
1950
1951             if not lastInfo then
1952                 print("failed to get last info for app" .. msg.appid)
1953                 return
1954             end
1955
1956             reply = m.JSON({
1957                 appid = msg.appid,
1958                 command = cmd,
1959                 arg = m.JSON({
1960                     lastinfo = lastinfo
1961                 })
1962             })
1963
1964             if sc:send_message(msg.appid, reply) then
1965                 print('*** reply OK')
1966             else
1967                 print('*** reply FAILED')
1968             end
1969
1970         elseif msg.command == 0x30004 then -- set_lastinfo
1971             print("command SET_LASTINFO")
1972             if not msg.arg or not msg.appid then
1973                 print("invalid message")
1974                 return
1975             end
1976
1977             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1978         end
1979     end
1980
1981     sc.resource_handler = function (self, cid, msg)
1982         if verbose > 0 then
1983             print('### ==> resource handler: ' .. command_name(msg.command))
1984             if verbose > 1 then
1985                 print(msg)
1986             end
1987         end
1988
1989         getKey = function (msg)
1990             -- Field rset.key is an id for distinguishing between rsets. All
1991             -- resource types appear to contain a different key. Just pick one
1992             -- based on what we have on the message.
1993
1994             key = nil
1995
1996             if msg and msg.res then
1997                 if msg.res.sound then
1998                     key = msg.res.sound.id
1999                 elseif msg.res.input then
2000                     key = msg.res.input.name
2001                 elseif msg.res.window then
2002                     key = msg.res.window.resourceId
2003                 end
2004             end
2005
2006             return key
2007         end
2008
2009         createResourceSet = function (ctl, client, msg)
2010             cb = function(rset, data)
2011
2012                 -- m:info("*** resource_cb: client = '" .. msg.appid .. "'")
2013
2014                 -- type is either basic (0) or interrupt (1)
2015                 requestType = 0
2016                 if msg.res.type then
2017                     requestType = msg.res.type
2018                 end
2019
2020                 if data.filter_first then
2021                     data.filter_first = false
2022                     return
2023                 end
2024
2025                 if rset.acquired then
2026                     cmd = 0x00040001 -- acquire
2027
2028                     if msg.appid == onscreen then
2029                         -- notifications are valid only for a number of seconds
2030                         rset.timer = m:Timer({
2031                             interval = 5000,
2032                             oneshot = true,
2033                             callback = function (t, data)
2034                                 m:info("notification resource set timer expired")
2035
2036                                 -- destroy the internal resource set
2037
2038                                 rset:destroy()
2039
2040                                 -- Send a "RELEASE" message to client.
2041                                 -- This triggers the resource deletion
2042                                 -- cycle in OnScreen.
2043
2044                                 reply = m.JSON({
2045                                     appid = msg.appid,
2046                                     command = cmd,
2047                                     res = {
2048                                         type = requestType
2049                                     }
2050                                 })
2051
2052                                 if rset.data.window then
2053                                     reply.res.window = rset.data.window
2054                                 end
2055
2056                                 sc:send_message(client, reply)
2057                             end
2058                         })
2059                     end
2060                 else
2061                     cmd = 0x00040002 -- release
2062                     if rset.timer then
2063                        rset.timer.callback = nil
2064                     end
2065                 end
2066
2067                 reply = m.JSON({
2068                         appid = msg.appid,
2069                         command = cmd,
2070                         res = {
2071                             type = requestType
2072                         }
2073                     })
2074
2075                 if rset.data.sound then
2076                     reply.res.sound = rset.data.sound
2077                 end
2078
2079                 if rset.data.window then
2080                     reply.res.window = rset.data.window
2081                 end
2082
2083                 if rset.data.input then
2084                     reply.res.input = rset.data.input
2085                 end
2086
2087                 if rset.acquired then
2088                     m:info("resource cb: 'acquire' reply to client " .. client)
2089                 else
2090                     m:info("resource cb: 'release' reply to client " .. client)
2091                 end
2092
2093                 if not sc:send_message(client, reply) then
2094                     m:info('*** reply FAILED')
2095                 end
2096             end
2097
2098             rset = m:ResourceSet({
2099                     application_class = "player",
2100                     zone = "driver", -- msg.zone ("full")
2101                     callback = cb
2102                 })
2103
2104             rset.data = {
2105                 cid = cid,
2106                 ctl = ctl,
2107                 filter_first = true
2108             }
2109
2110             if msg.res.sound then
2111                 rset:addResource({
2112                         resource_name = "audio_playback"
2113                     })
2114                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
2115                 rset.resources.audio_playback.attributes.appid = msg.appid
2116                 print("sound name: " .. msg.res.sound.name)
2117                 print("sound zone:" .. msg.res.sound.zone)
2118                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
2119
2120                 rset.data.sound = msg.res.sound
2121             end
2122
2123             if msg.res.input then
2124                 rset:addResource({
2125                         resource_name = "input"
2126                     })
2127                 rset.resources.input.attributes.pid = tostring(msg.pid)
2128                 rset.resources.input.attributes.appid = msg.appid
2129                 print("input name: " .. msg.res.input.name)
2130                 print("input event:" .. tostring(msg.res.input.event))
2131
2132                 rset.data.input = msg.res.input
2133             end
2134
2135             if msg.res.window then
2136                 rset:addResource({
2137                         resource_name = "screen",
2138                         shared = true
2139                     })
2140                 rset.resources.screen.attributes.pid = tostring(msg.pid)
2141                 rset.resources.screen.attributes.appid = msg.appid
2142                 rset.resources.screen.attributes.surface = msg.res.window.resourceId
2143                 complete_area = msg.res.window.display .. '.' .. msg.res.window.area
2144                 rset.resources.screen.attributes.area = complete_area
2145                 if msg.appid == onscreen then
2146                     rset.resources.screen.attributes.classpri = 1
2147                 else
2148                     rset.resources.screen.attributes.classpri = 0
2149                 end
2150
2151                 rset.data.window = msg.res.window
2152             end
2153
2154             rset.key = getKey(msg)
2155
2156             return rset
2157         end
2158
2159         -- parse the message
2160
2161         -- fields common to all messages:
2162         --      msg.command
2163         --      msg.appid
2164         --      msg.pid
2165
2166         if msg.command == 0x40011 then -- create_res
2167             print("command CREATE_RES")
2168             key = getKey(msg)
2169
2170             if key then
2171                 if not sets.cid then
2172                     sets.cid = {}
2173                 end
2174                 sets.cid.key = createResourceSet(self, cid, msg)
2175             end
2176
2177         elseif msg.command == 0x40012 then -- destroy_res
2178             print("command DESTROY_RES")
2179             key = getKey(msg)
2180
2181             if key then
2182                 if sets.cid and sets.cid[key] then
2183                     sets.cid[key]:release()
2184                     sets.cid[key] = nil -- garbage collecting
2185                 end
2186             end
2187
2188         elseif msg.command == 0x40001 then -- acquire_res
2189             print("command ACQUIRE_RES")
2190             key = getKey(msg)
2191
2192             if key then
2193                 print("key is " .. tostring(key))
2194                 if not sets.cid then
2195                     sets.cid = {}
2196                 end
2197                 if not sets.cid[key] then
2198                     print("creating a resource set")
2199                     sets.cid[key] = createResourceSet(self, cid, msg)
2200                 end
2201                 print("acquiring the resource set")
2202                 sets.cid[key]:acquire()
2203             end
2204
2205         elseif msg.command == 0x40002 then -- release_res
2206             print("command RELEASE_RES")
2207
2208             key = getKey(msg)
2209
2210             if key then
2211                 if sets.cid and sets.cid[key] then
2212                     sets.cid[key]:release()
2213                 end
2214
2215                 if msg.appid == onscreen then
2216                     -- in case of OnScreen, this actually means that the
2217                     -- resource set is never used again; let gc do its job
2218                     if sets.cid and sets.cid[key] then
2219                         sets.cid[key] = nil -- garbage collecting
2220                     end
2221                 end
2222
2223             end
2224
2225
2226         elseif msg.command == 0x40003 then -- deprive_res
2227             print("command DEPRIVE_RES")
2228
2229         elseif msg.command == 0x40004 then -- waiting_res
2230             print("command WAITING_RES")
2231
2232         elseif msg.command == 0x40005 then -- revert_res
2233             print("command REVERT_RES")
2234         end
2235     end
2236
2237     sc.inputdev_handler = function (self, cid, msg)
2238         if verbose > 0 then
2239             print('*** inputdev handler: ' .. command_name(msg.command))
2240             if verbose > 1 then
2241                 print(msg)
2242             end
2243         end
2244     end
2245
2246     sc.notify_handler = function (self, cid, msg)
2247         if verbose > 0 then
2248             print('### notify handler: ' .. command_name(msg.command))
2249             if verbose > 1 then
2250                 print(msg)
2251             end
2252         end
2253     end
2254 end
2255
2256 function send_driving_mode_to(client)
2257     if client == "" then
2258         return
2259     end
2260
2261     local driving_mode = mdb.select.select_driving_mode.single_value
2262
2263     if not driving_mode then driving_mode = 0 end
2264
2265     local reply = m:JSON({ command = 0x60001,
2266                            arg     = m:JSON({ stateid = 1,
2267                                               state   = driving_mode
2268                                      })
2269                   })
2270
2271     if verbose > 0 then
2272         print("### <== sending " .. command_name(reply.command) .. " message")
2273         if verbose > 1 then
2274             print(reply)
2275         end
2276     end
2277
2278     sc:send_message(client, reply)
2279 end
2280
2281 function send_night_mode_to(client)
2282     if client == "" then
2283         return
2284     end
2285
2286     local night_mode = mdb.select.select_night_mode.single_value
2287
2288     if not night_mode then night_mode = 0 end
2289
2290     local reply = m:JSON({ command = 0x60001,
2291                            arg     = m:JSON({ stateid = 2,
2292                                               state   = night_mode
2293                                      })
2294                   })
2295
2296     if verbose > 0 then
2297         print("### <== sending " .. command_name(reply.command) .. " message")
2298         if verbose > 1 then
2299             print(reply)
2300         end
2301      end
2302
2303      sc:send_message(client, reply)
2304 end
2305
2306 -- we should have 'audio_playback' defined by now
2307 m:try_load_plugin('telephony')